<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP 5.1.6 or newer
 *
 * @package		CodeIgniter
 * @author		ExpressionEngine Dev Team
 * @copyright	Copyright (c) 2008 - 2011, EllisLab, Inc.
 * @license		http://codeigniter.com/user_guide/license.html
 * @link		http://codeigniter.com
 * @since		Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * CodeIgniter JNumber Helpers
 *
 * @package		CodeIgniter
 * @subpackage	Helpers
 * @category	Helpers
 * @author		NGUYENDUYTRUNG Dev Team
 * @link		http://codeigniter.com/user_guide/helpers/number_helper.html
 */

// ------------------------------------------------------------------------

/**
 * Formats a numbers as bytes, based on size, and adds the appropriate suffix
 *
 * @access	public
 * @param	mixed	// will be cast as int
 * @return	string
 */
if ( ! function_exists('decimal_format'))
{
	function decimal_format($num)
	{
        $vt = strpos($num,'.');//var_dump($vt);
        if($vt !== false){
            $sl = (int)substr($num, $vt+1);//var_dump($sl);
            if($sl != 0){
                return $num;
            }else{
                return (int)$num;
            }
        }
		return $num;
	}
}

/*
* Ham kiem tra xem co phai la chu so hay ko
*/
if ( ! function_exists('numeric_check'))
{
    function numeric_check($number){
        $isnumber = (bool)preg_match( '/^[\-+]?[0-9]*\.?[0-9]+$/', $number);
        if( !$isnumber ){
            return false;
        }
        return true;
    }
}

/*
* Ham kiem tra xem ngay thang nam co hop le ko
*/
if ( ! function_exists('date_check'))
{
    function date_check($sDate){
        $flag = true;
        $sDate = str_replace(' ', '-', $sDate);
        $sDate = str_replace('/', '-', $sDate);
        $sDate = str_replace('--', '-', $sDate);
        
        $arr = explode('-',$sDate);
        if(count($arr) == 3){
            if(strlen($arr[0]) != 4 ){
                $flag = false;
            }
            if(strlen($arr[1]) != 2 ){
                $flag = false;
            }
            if(strlen($arr[2]) != 2 ){
                $flag = false;
            }
        }else{
            $flag = false;
        }
        
        if($flag == true){
            preg_match('/^(\d{4})-(\d{2})-(\d{2})$/', $sDate, $xadBits);
            return checkdate($xadBits[2], $xadBits[3], $xadBits[1]);
        }else{
            return false;
        }
    }
}

if ( ! function_exists('checkDateVn'))
{
    function checkDateVn($sDate){
        $flag = true;
        $sDate = str_replace(' ', '-', $sDate);
        $sDate = str_replace('/', '-', $sDate);
        $sDate = str_replace('--', '-', $sDate);
        
        $arr = explode('-',$sDate);
        if(count($arr) == 3){
            if(strlen($arr[2]) != 4 ){
                $flag = false;
            }
            if(strlen($arr[1]) != 2 ){
                $flag = false;
            }
            if(strlen($arr[0]) != 2 ){
                $flag = false;
            }
        }else{
            $flag = false;
        }
        
        if($flag == true){
            preg_match('/^(\d{2})-(\d{2})-(\d{4})$/', $sDate, $xadBits);
            return checkdate($xadBits[2], $xadBits[1], $xadBits[3]);
        }else{
            return false;
        }
    }
}

if ( ! function_exists('dateMysql'))
{
    function dateMysqlReplace($sDate){
        $sDate = str_replace(' ', '-', $sDate);
        $sDate = str_replace('/', '-', $sDate);
        $sDate = str_replace('--', '-', $sDate);
        
        return $sDate;
    }
}
if ( ! function_exists('dateVnToMysql'))
{
    function dateVnToMysql($sDate){
        $sDate = str_replace(' ', '-', $sDate);
        $sDate = str_replace('/', '-', $sDate);
        $sDate = str_replace('--', '-', $sDate);
        $arr = explode('-',$sDate);
        return $arr[2].'-'.$arr[1].'-'.$arr[0];
    }
}

if ( ! function_exists('dateMysqlToVN'))
{
    function dateMysqlToVN($sDate,$type='date'){
        $str = '';
        if($sDate == null || $sDate == '0000-00-00 00:00:00' || $sDate == ''){
            $str = '-';
        }else{
            if($type=='full'){
                $str = strftime('%d/%m/%Y %H:%M',strtotime($sDate));
                //$str = strftime('%d/%m/%Y %H:%M:%S',strtotime($sDate));
            }else{
                $str = strftime('%d/%m/%Y',strtotime($sDate));
            }
        }
        return $str;
    }
}

if ( ! function_exists('formatMoney'))
{
    function formatMoney($number) {
        $num = '';
        $last = '';
        $dot_pos = strpos($number,".");
        if($dot_pos === false){
            $num = $number;
        }else{
            $num = substr($number, 0, $dot_pos);
            $last = substr($number, $dot_pos+1);
        }
        while (true) {
            $replaced = preg_replace('/(-?\d+)(\d\d\d)/', '$1,$2', $num);
            if ($replaced != $num) {
                $num = $replaced;
            } else {
                break;
            }
        }
        if($last != ''){
            $number = $num.'.'.$last;
        }else{
            $number = $num;
        }
        return $number;
    }
}

if ( ! function_exists('formatMoney_strvn'))
{
    function formatMoney_strvn($number) {
        $str_number = (string)$number;
        $str_len = strlen($str_number) - 1;
        $arr_number = array();
        $dem = 0;
        for($i = $str_len; $i >= 0;$i--){
            if( $dem == 0 ){$arr_number['htram']['dv'] = $str_number[$i];}
            if( $dem == 1 ){$arr_number['htram']['chuc'] = $str_number[$i];}
            if( $dem == 2 ){$arr_number['htram']['tram'] = $str_number[$i];}
            if( $dem == 3 ){$arr_number['hngan']['dv'] = $str_number[$i];}
            if( $dem == 4 ){$arr_number['hngan']['chuc'] = $str_number[$i];}
            if( $dem == 5 ){$arr_number['hngan']['tram'] = $str_number[$i];}
            if( $dem == 6 ){$arr_number['htrieu']['dv'] = $str_number[$i];}
            if( $dem == 7 ){$arr_number['htrieu']['chuc'] = $str_number[$i];}
            if( $dem == 8 ){$arr_number['htrieu']['tram'] = $str_number[$i];}
            if( $dem == 9 ){$arr_number['hty']['dv'] = $str_number[$i];}
            if( $dem == 10 ){$arr_number['hty']['chuc'] = $str_number[$i];}
            if( $dem == 11 ){$arr_number['hty']['tram'] = $str_number[$i];}
            $dem++;
        }
        
        $chuoi_so = '';
        $hty = false;
        $htrieu = false;
        $hngan = false;
        $htram = false;
        $arr_str_number = array();
        if( isset($arr_number['hty']) ){
            $hty = true;
            $chuoi_so .= string_number_vn($arr_number['hty'], 'hty',$arr_str_number);
        }
        if( isset($arr_number['htrieu']) ){
            $htrieu = true;
            $chuoi_so .= ' '.string_number_vn($arr_number['htrieu'], 'htrieu',$arr_str_number);
        }
        if( isset($arr_number['hngan']) ){
            $hngan = true;
            $chuoi_so .= ' '.string_number_vn($arr_number['hngan'], 'hngan',$arr_str_number);
        }
        if( isset($arr_number['htram']) ){
            $htram = true;
            $chuoi_so .= ' '.string_number_vn($arr_number['htram'], 'htram',$arr_str_number);
        }
        //var_dump($arr_str_number);
        if(count($arr_str_number) > 0){
            $str = '';
            foreach($arr_str_number As $key=>$item){
                if($key != 0){
                    $str .= ' ';
                }
                $str .= $item;
            }
            return $str;
        }else{
            return 'Không';
        }
    }
}

if ( ! function_exists('string_number_vn'))
{
    function string_number_vn($arr_number,$donvi = 'htram',&$arr_str_number) {
        $doc_so = array('không','một','hai','ba','bốn','năm','sáu','bảy','tám','chín');
        $doc_dv = array('htram'=>'','hngan'=>'ngàn','htrieu'=>'triệu','hty'=> 'tỷ');
        $chuoi_so = '';
        $flag = false;
        $chuc_le = false;
        $count = 0;
        $chuc_noise = false;

        if( isset($arr_number['tram']) && (int)$arr_number['tram'] != 0 ){
            $flag = true;
        }
        if( isset($arr_number['chuc']) && (int)$arr_number['chuc'] != 0 ){
            $flag = true;
        }
        if( isset($arr_number['dv']) && (int)$arr_number['dv'] != 0 ){
            $flag = true;
        }
        if($flag == false){
            return '';
        }
        
        if( isset($arr_number['tram']) ){
            $count ++;
            $chuoi_so .= $doc_so[$arr_number['tram']].' trăm ';
            $arr_str_number[] = $doc_so[$arr_number['tram']];
            $arr_str_number[] = 'trăm';
        }
        if( isset($arr_number['chuc']) ){
            $count ++;
            if($arr_number['chuc'] == 0){                
                if( isset($arr_number['dv']) ){
                    if($arr_number['dv'] != 0){
                        $chuoi_so .= ' lẻ ';
                        $arr_str_number[] = 'lẻ';
                        $chuc_le = true;
                    }
                }
            }elseif($arr_number['chuc'] == 1){
                $chuoi_so .= ' mười ';
                $arr_str_number[] = 'mười';
                $chuc_noise = false;
            }
            else{
                $chuoi_so .= $doc_so[$arr_number['chuc']].' mươi ';
                $arr_str_number[] = $doc_so[$arr_number['chuc']];
                $arr_str_number[] = 'mươi';
                $chuc_noise = true;
            }
        }
        if( isset($arr_number['dv']) ){
            $count ++;
            if($arr_number['dv'] != 0){
                //if($arr_number['dv'] == 1 && $count < 1){
                if($arr_number['dv'] == 1 && $chuc_noise == true){
                    $chuoi_so .= ' mốt ';
                    $arr_str_number[] = 'mốt';
                }elseif($arr_number['dv'] == 5 && $chuc_le == false){
                    $chuoi_so .= ' lăm ';
                    $arr_str_number[] = 'lăm';
                }else{
                    $chuoi_so .= $doc_so[$arr_number['dv']];
                    $arr_str_number[] = $doc_so[$arr_number['dv']];
                }
            }
        }
        
        $chuoi_so .= ' '.$doc_dv[$donvi];
        if($doc_dv[$donvi] != ''){
            $arr_str_number[] = $doc_dv[$donvi];
        }
        return $chuoi_so;

    }
}
/* End of file number_helper.php */
/* Location: ./system/helpers/number_helper.php */